#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import unicode_literals, division, absolute_import, print_function
from calibre.web.feeds.news import BasicNewsRecipe
import urllib,urllib2
import re
import json

class SpringCloudCSDN(BasicNewsRecipe):
    title = '史上最简单的 SpringCloud 教程'
    __author__ = '方志朋'
    description = '''史上最简单的 SpringCloud 教程'''
    timefmt = '[%Y-%m-%d]'
    no_stylesheets = True
    INDEX = 'http://blog.csdn.net/forezp/article/category/6830968/'
    BASEURL = 'http://blog.csdn.net/'
    IndexUrl = 'https://docs.microsoft.com/zh-cn/aspnet/core/toc.json'
    auto_cleanup = True                   # 如果没有手动分析文章结构，可以考虑开启该选项自动清理正文内容
    #remove_tags = [dict(attrs={'class': ['sxs-lookup']})]
    language = 'zh-CN'
    keep_only_tags = [{'class': ['markdown_views']}]  # 仅保留文章的post中的内容，其中为自己分析得到的正文范围
    max_articles_per_feed = 10000           # 默认最多文章数是100，可改为更大的数字以免下载不全

   

    def parse_index(self):
        start_page = 1      # int(pages_info[1])
        end_page = 2  
        articles = []
        for p in range(start_page, end_page + 1):
            print('page link:', self.INDEX + str(p))
            soup_page = self.index_to_soup(self.INDEX + str(p))
            soup_titles = soup_page.findAll(**{'class': 'link_title'})
            for soup_title in soup_titles:
                href = soup_title.a
                articles.append({'title':href.next, 'url':self.BASEURL + href['href']})

        articles.reverse()
         
        print('articles:', articles)
        res = [ (u'史上最简单的 SpringCloud 教程', articles)]    # 返回tuple，分别是电子书名字和文章列表
        #self.abort_recipe_processing('test')  # 用来中断电子书生成，调试用
        return res